package com.sky.controller.admin;


import com.sky.result.Result;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Slf4j
@RestController("adminShopController")
@RequestMapping("/admin/shop")
public class ShopController {
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    public static final String KEY_SHOP_STATUS = "SHOP_STATUS";

    /**
     * 设置营业状态
     * @param status
     * @return
     */
    @PutMapping("/{status}")
    @ApiOperation("设置营业状态")
    public Result setStatus(@PathVariable Integer status){
        log.info("设置营业状态：{}", status==1?"营业中" : "打烊中");
        stringRedisTemplate.opsForValue().set(KEY_SHOP_STATUS, status+"");
        return Result.success();
    }


    /**
     * 查询营业状态
     * @return
     */
    @GetMapping("/status")
    @ApiOperation("查询营业状态")
    public Result<Integer> getStatus(){
        log.info("查询营业状态");
        String status = stringRedisTemplate.opsForValue().get(KEY_SHOP_STATUS);
        if (status == null) {
            return Result.error("查询不到");
        }
        log.info("营业状态为：{}", Integer.parseInt(status)==1?"营业中" : "打烊中");
        return Result.success(Integer.parseInt(status));
    }
}
